#!/usr/bin/env python
# encoding: utf-8
"""
@author: youfeng
@email: youfeng243@163.com
@license: Apache Licence 
@file: kafka_consumer.py
@time: 2016/12/19 16:34
"""
from pykafka import KafkaClient

# client = KafkaClient(hosts="182.61.44.214:9092,10.66.246.141:9092,182.61.26.153:9092")  # 可接受多个Client这是重点
client = KafkaClient(hosts='172.18.180.223:9092,172.18.180.222:9092,172.18.180.225:9092')  # 可接受多个Client这是重点

print client.topics  # 查看所有topic
topic = client.topics['test']  # 选择一个topic
balanced_consumer = topic.get_balanced_consumer(
    'test_group',
    auto_commit_enable=True,
    zookeeper_connect='172.18.180.223:2181,172.18.180.222:2181,172.18.180.225:2181',
)
for message in balanced_consumer:
    # print message
    if message is not None:
        # 打印接收到的消息体的偏移个数和值
        print message.offset, message.value
